using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using RpgXna.Model.XnaClassesWrappers;

namespace RpgXna.Model.Helpers
{
    internal static class Movements
    {
        private const int MoveDown = 1;
        private const int MoveLeft = -1;
        private const int MoveRight = 1;
        private const int MoveUp = -1;

        private static float Rotate(IKeyboardStateWrapper aCurrentKeyboardState, float currentRotation)
        {
            if (aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Right) && aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Down))
            {
                return MathHelper.ToRadians(135);
            }
            if (aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Left) && aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Down))
            {
                return MathHelper.ToRadians(225);
            }
            if (aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Left) && aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Up))
            {
                return MathHelper.ToRadians(315);
            }
            if (aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Right) && aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Up))
            {
                return MathHelper.ToRadians(45);
            }

            if (aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Right))
            {
                return MathHelper.ToRadians(90);
            }
            if (aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Down))
            {
                return MathHelper.ToRadians(180);
            }
            if (aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Left))
            {
                return MathHelper.ToRadians(270);
            }
            if (aCurrentKeyboardState.IsKeyDown(InputBindings.Bindings.Up))
            {
                return MathHelper.ToRadians(0);
            }

            return currentRotation;
        }

        public static MoveWrapper Move(Vector2 currentPosition, float currentRotation, IKeyboardStateWrapper keyboardState,
                                       int charSpeed, GameTime theGameTime, bool forward)
        {
            float x = currentPosition.X;
            float y = currentPosition.Y;
            var posCopy = new Vector2(x, y);
            Vector2 speed = Vector2.Zero;
            Vector2 direction = Vector2.Zero;
            int realSpeed = charSpeed;
            if (keyboardState.IsKeyDown(InputBindings.Bindings.Left))
            {
                speed.X = realSpeed;
                direction.X = MoveLeft * ((forward) ? 1 : -1);
            }
            else if (keyboardState.IsKeyDown(InputBindings.Bindings.Right))
            {
                speed.X = realSpeed;
                direction.X = MoveRight * ((forward) ? 1 : -1);
            }

            if (keyboardState.IsKeyDown(InputBindings.Bindings.Up))
            {
                speed.Y = realSpeed;
                direction.Y = MoveUp * ((forward) ? 1 : -1);
            }
            else if (keyboardState.IsKeyDown(InputBindings.Bindings.Down))
            {
                speed.Y = realSpeed;
                direction.Y = MoveDown * ((forward) ? 1 : -1);
            }

            posCopy += direction * speed * (float)theGameTime.ElapsedGameTime.TotalSeconds;

            return new MoveWrapper
                       {
                           Speed = speed,
                           Rotation = Rotate(keyboardState, currentRotation),
                           Direction = direction,
                           Position = posCopy
                       };
        }
    }
}